/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Customer;
import Model.User;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author gulben
 */
@ManagedBean(name="loginController")
@SessionScoped
public class LoginController implements Serializable{

    public String username;
    public String password;
    public boolean isExistUser;
    
    public LoginController() {
        
    }
    
    public String getUsername() {
        return username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    
    public String login() throws SQLException, NamingException{
       String page;
       int resultSet = checkUser(username, password);
       if(resultSet == 1) {
            isExistUser = true;
            Customer c = getUserId(username);
            User user = new User(c.getId(), username, password);
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", user);
        } else {
            isExistUser = false;
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        }
       if(isExistUser == true) {
           page = "mainpage";
       } else {
           page = "login";
           addMessage("Hata", "Ya şifreniz yada kullanıcı adınız hatalı");
       }
       return page;
    }
    
    public Customer getUserId(String username) {
        EntityManager entityManager =  Persistence.createEntityManagerFactory("KitapcdappPU").createEntityManager();
        Query query = entityManager.createNamedQuery("Customer.findByCustomerUsername");
        query.setParameter("username", username);
        Customer customer = (Customer) query.getSingleResult();
        return customer;
    }
    
    public String logout() {
       FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
       return "login";
    }
  
    public int checkUser(String username, String password) throws SQLException, NamingException
    {
        try {
            EntityManager entityManager =  Persistence.createEntityManagerFactory("KitapcdappPU").createEntityManager();
            Query query = entityManager.createNamedQuery("Customer.findByCustomerCount");
            query.setParameter("username", username);
            query.setParameter("password", password);
            List<Customer> customer = (List<Customer>)query.getResultList();
            return customer.size();
        } finally {
        }
    }

    private void addMessage(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
    
    public User getUser() {
        Map<String, Object> session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        User user = (User) session.get("user");
        return user;
    }
}
